import Taro from '@tarojs/taro';

import {Command, GAME_RPICE_TYPE, GAME_TYPE} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async status() {
      // 或取当前状态值
      let {
        main: {isLoading},
      } = getData();
      action.commonChange('main.isLoading', !isLoading);
    },

    /**
     * state值更新
     * @param payload
     */
    async stateCommonChange(payload) {
      const {stateName, value} = payload;
      action.commonChange(stateName, value);
    },

    /**
     * 查询新人保障
     */
    async loadUserIsNewComer(payload) {
      console.log('action.tsx loadUserIsNewComer payload', payload);

      action.commonChange('main.response.isNewComer', '');
      action.commonChange('main.status.loadingIsNewComer', true);

      // 编辑参数
      let params = {
        ...payload,
        gameType: GAME_TYPE,
      };

      let {data, code, message} = await api.SyntheticCardController.userIsNewComer(params);
      console.log('action.ts loadUserIsNewComer data', data);

      let {
        main: {userInfo},
      } = getData();

      userInfo = {
        ...userInfo,
        isNewComer: data,
      };

      console.log('action.ts userinFO', userInfo);

      action.commonChange('main.userInfo', userInfo);
      action.commonChange('main.response.isNewComer', code);
      action.commonChange('main.status.loadingIsNewComer', false);
    },

    /**
     * 抽奖
     */
    async gameLottery(payload) {
      console.log('action.ts gameLottery payload', payload);

      action.commonChange('main.codeMessage', '');
      action.commonChange('main.gameLotteries', []);
      action.commonChange('main.response.gameLottery', '');
      action.commonChange('main.status.loadingGameLottery', true);

      // 编辑参数
      let params = {
        ...payload,
        gameType: GAME_TYPE,
      };

      console.log('action.js synthetciDrawCard params', params);
      let {data, code, message} = await api.SyntheticCardController.gameLottery(params);
      console.log('action.ts gameLottery data', data);

      //0-商品 1-优惠券 2-魔力币 3-道具 4-卡片 (卡片平铺，其他都统计)
      data = data || [];
      let result = data.filter(
        (i) =>
          i.prizeType != null &&
          i.prizeType != GAME_RPICE_TYPE['ticket'] &&
          i.prizeType != GAME_RPICE_TYPE['gold'] &&
          i.prizeType != GAME_RPICE_TYPE['prop'],
      );

      if (data && data.length > 0) {
        // 商品
        // let cardItems = data.filter((i) => i.prizeType == GAME_RPICE_TYPE['card']);
        // if (cardItems.length > 0) {
        //   let collectionIds = [];
        //   cardItems.map((i) => {
        //     collectionIds.push(i.collectionId);
        //   });
        //   result.push({
        //     id: cardItems.length,
        //     prizeType: GAME_RPICE_TYPE['card'],
        //     count: cardItems.length,
        //     collectionIds,
        //   });
        // }

        // 优惠券筛选
        let tickItems = data.filter((i) => i.prizeType == GAME_RPICE_TYPE['ticket']);
        if (tickItems.length > 0) {
          result.push({
            id: tickItems.length,
            prizeType: GAME_RPICE_TYPE['ticket'],
            count: tickItems.length,
          });
        }

        console.log('action.tsx synthetcit tickImts', tickItems);

        // 道具
        let propItems = data.filter((i) => i.prizeType == GAME_RPICE_TYPE['prop']);
        if (propItems.length > 0) {
          result.push({
            id: propItems.length,
            prizeType: GAME_RPICE_TYPE['prop'],
            count: propItems.length,
          });
        }

        // 金币筛选
        let goldItems = data.filter((i) => i.prizeType == GAME_RPICE_TYPE['gold']);

        console.log('action.tsx synthetcit goldItems', goldItems);

        let goldNum = goldItems.reduce((goldNum, currentItem) => {
          currentItem.magicCoin = currentItem.magicCoin || 0;
          return goldNum + currentItem.magicCoin;
        }, 0);

        if (goldItems.length > 0) {
          result.push({
            id: result.length + 1,
            prizeType: GAME_RPICE_TYPE['gold'],
            count: goldNum,
          });
        }
      }
      console.log('actin.ts gameLottery result', result);
      action.commonChange('main.codeMessage', message);
      action.commonChange('main.gameLotteries', result);
      action.commonChange('main.response.gameLottery', code);
      action.commonChange('main.status.loadingGameLottery', false);
    },

    /**
     * 完成任务
     * @param payload
     */
    async computedTask(payload) {
      action.commonChange('main.response.computedTask', '');
      action.commonChange('main.status.computingTask', true);

      // 编辑参数
      let params = {
        ...payload,
      };
      let {data, code, message} = await api.GameTaskController.computedTask(params);
      action.commonChange('main.response.computedTask', code);
      action.commonChange('main.status.computingTask', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageSyntheticDrawCardPaMain'),
  };
}
